/** Image Layers
 *
 * Created : Jan 25, 2015
 *
 * @author pquiring
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayersWindow extends javax.swing.JFrame implements ActionListener {

  /**
   * Creates new form ImageLayers
   */
  public LayersWindow(MainPanel mainPanel) {
    this.mainPanel = mainPanel;
//    setUndecorated(true);
    initComponents();
    setAlwaysOnTop(true);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    panel = new javax.swing.JPanel();
    jToolBar1 = new javax.swing.JToolBar();
    bAdd = new javax.swing.JButton();
    bDel = new javax.swing.JButton();
    bDup = new javax.swing.JButton();
    bMerge = new javax.swing.JButton();
    bUp = new javax.swing.JButton();
    bDown = new javax.swing.JButton();
    bEdit = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Image Layers");
    setFocusable(false);
    setFocusableWindowState(false);
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosed(java.awt.event.WindowEvent evt) {
        formWindowClosed(evt);
      }
    });

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 0, Short.MAX_VALUE)
    );
    panelLayout.setVerticalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 279, Short.MAX_VALUE)
    );

    jToolBar1.setFloatable(false);
    jToolBar1.setRollover(true);

    bAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/addlayer.png"))); // NOI18N
    bAdd.setToolTipText("Add Layer");
    bAdd.setFocusable(false);
    bAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bAdd.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bAddActionPerformed(evt);
      }
    });
    jToolBar1.add(bAdd);

    bDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dellayer.png"))); // NOI18N
    bDel.setToolTipText("Delete Layer");
    bDel.setFocusable(false);
    bDel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bDel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bDel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bDelActionPerformed(evt);
      }
    });
    jToolBar1.add(bDel);

    bDup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/duplayer.png"))); // NOI18N
    bDup.setToolTipText("Duplicate Layer");
    bDup.setFocusable(false);
    bDup.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bDup.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bDup.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bDupActionPerformed(evt);
      }
    });
    jToolBar1.add(bDup);

    bMerge.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mergelayer.png"))); // NOI18N
    bMerge.setToolTipText("Merge Layer Up");
    bMerge.setFocusable(false);
    bMerge.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bMerge.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bMerge.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bMergeActionPerformed(evt);
      }
    });
    jToolBar1.add(bMerge);

    bUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/movelayerup.png"))); // NOI18N
    bUp.setToolTipText("Move Layer Up");
    bUp.setFocusable(false);
    bUp.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bUp.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bUp.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bUpActionPerformed(evt);
      }
    });
    jToolBar1.add(bUp);

    bDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/movelayerdown.png"))); // NOI18N
    bDown.setToolTipText("Move Layer Down");
    bDown.setFocusable(false);
    bDown.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bDown.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bDown.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bDownActionPerformed(evt);
      }
    });
    jToolBar1.add(bDown);

    bEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editlayer.png"))); // NOI18N
    bEdit.setToolTipText("Edit Layer Properties");
    bEdit.setFocusable(false);
    bEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    bEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    bEdit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bEditActionPerformed(evt);
      }
    });
    jToolBar1.add(bEdit);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    mainPanel.layers = null;
  }//GEN-LAST:event_formWindowClosed

  private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
    mainPanel.addLayer();
    setup(pc);
  }//GEN-LAST:event_bAddActionPerformed

  private void bDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDelActionPerformed
    mainPanel.delLayer();
    setup(pc);
  }//GEN-LAST:event_bDelActionPerformed

  private void bEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditActionPerformed
    mainPanel.editLayer();
    setup(pc);
  }//GEN-LAST:event_bEditActionPerformed

  private void bDupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDupActionPerformed
    mainPanel.dupLayer();
    setup(pc);
  }//GEN-LAST:event_bDupActionPerformed

  private void bMergeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bMergeActionPerformed
    mainPanel.mergeLayer();
    setup(pc);
  }//GEN-LAST:event_bMergeActionPerformed

  private void bUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpActionPerformed
    mainPanel.moveLayerUp();
    setup(pc);
  }//GEN-LAST:event_bUpActionPerformed

  private void bDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDownActionPerformed
    mainPanel.moveLayerDown();
    setup(pc);
  }//GEN-LAST:event_bDownActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bAdd;
  private javax.swing.JButton bDel;
  private javax.swing.JButton bDown;
  private javax.swing.JButton bDup;
  private javax.swing.JButton bEdit;
  private javax.swing.JButton bMerge;
  private javax.swing.JButton bUp;
  private javax.swing.JToolBar jToolBar1;
  private javax.swing.JPanel panel;
  // End of variables declaration//GEN-END:variables

  private MainPanel mainPanel;
  private ButtonGroup group;
  private PaintCanvas pc;
  private JRadioButton actives[];
  private JCheckBox shows[];

  public void setup(PaintCanvas pc) {
    this.pc = pc;
    group = new ButtonGroup();
    panel.removeAll();
    int cnt = pc.getImageLayers();
    panel.setLayout(new GridLayout(cnt, 2));
    Insets zero = new Insets(0,0,0,0);
    actives = new JRadioButton[cnt];
    shows = new JCheckBox[cnt];

    for(int a=0;a<cnt;a++) {
      JRadioButton active = new JRadioButton(pc.name[a]);
      active.setMargin(zero);
      active.putClientProperty("idx", a);
      active.putClientProperty("type", "active");
      active.addActionListener(this);
      active.setSelected(pc.getImageLayer() == a);
      actives[a] = active;
      panel.add(active);
      group.add(active);
      JCheckBox show = new JCheckBox();
      show.setMargin(zero);
      show.putClientProperty("idx", a);
      show.putClientProperty("type", "show");
      show.addActionListener(this);
      show.setSelected(pc.show[a]);
      active.putClientProperty("show", show);
      shows[a] = show;
      panel.add(show);
    }
    pack();
  }

  public void actionPerformed(ActionEvent e) {
    JComponent c = (JComponent)e.getSource();
    Integer idx = (Integer)c.getClientProperty("idx");
    String type = (String)c.getClientProperty("type");
    if (type.equals("active")) {
      if (pc.getImageLayer() == idx) return;
      pc.setImageLayer(idx);
      if (!pc.show[idx]) {
        JCheckBox cb = (JCheckBox)c.getClientProperty("show");
        cb.setSelected(true);
        pc.show[idx] = true;
      }
    } else if (type.equals("show")) {
      JCheckBox cb = (JCheckBox)c;
      int cidx = pc.getImageLayer();
      if (cidx == idx) {
        //can not hide active image
        cb.setSelected(true);
        return;
      }
      pc.show[idx] = cb.isSelected();
    }
    pc.repaint();
  }
}
